package com.example.demo.user;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import org.springframework.util.NumberUtils;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;

/**
 * 类描述：
 *
 * @author: huxinchao  38653
 * Create Date：
 * <p>
 * History:      huxinchao   Created.
 */
public class DataSerializerUtils extends JsonSerializer<BigDecimal> {
    @Override
    public void serialize(BigDecimal value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
        if(!StringUtils.isEmpty(value)) {
            DecimalFormat df = new DecimalFormat("#0.0000");
            df.setRoundingMode(RoundingMode.HALF_UP);
            gen.writeString(df.format(value));
        }else {//这个分支不要忘记了，否则将不输出这个属性的值
            gen.writeString("0.0000");
        }
    }
}
